package com.copro.chemdb.stat.rule.export;

import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.ihelpuoo.util.Properties;

import com.copro.chemdb.stat.ExportRule;
import com.copro.chemdb.stat.StatResult;
import com.copro.chemdb.subject.Subject;
import com.copro.simple.Application;
import com.copro.simple.SimpleException;

public class Rule6 implements ExportRule {

	@SuppressWarnings("unchecked")
	@Override
	public void doExport(HSSFWorkbook wb, Subject subject, StatResult re,
			short customStyle, Application app) throws SimpleException {
		Map<String, Object> result = (Map<String, Object>) re.getResult();
		String text = (String) result.get("text");
		if (text.length() > 30) {
			text = text.substring(0, 30);
		}
		int count = ((Integer) result.get("count")).intValue();
		// create new sheet
		HSSFSheet sheet = wb.createSheet("开发周期");
		HSSFRow row = sheet.createRow(0);
		CellUtil.createCell(row, 0, "有效记录数: " + count);
		// set the header
		row = sheet.createRow(1);
		CellUtil.createCell(row, 0, "单位名称", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 1, "数据库名称", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 2, "开发时间", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 3, "建成时间", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 4, "开发周期(月)", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		// set body
		// set the data body
		List<List<String>> rows = (List<List<String>>) result.get("rows");
		for (int i = 0; i < rows.size(); i++) {
			List<String> cells = rows.get(i);
			row = sheet.createRow(i + 2);
			for (int j = 0; j < cells.size(); j++) {
				CellUtil.createCell(row, j, cells.get(j));
			}
		}
	}

	@Override
	public void setup(Properties conf) {
	}

}
